package com.example.questionario;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.questionario.control.ControlaSlides;

public class Pag2_1 extends Activity implements Pagina{
	private int qtdPerguntas= 2;
	RadioGroup[] pergs= new RadioGroup[qtdPerguntas];
	int[] resps= null;
	String idPag= "Pag2_1";
	RadioButton semFilhos;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pag2_1);
		Button next= (Button) findViewById(R.id.next);
		Button back= (Button) findViewById(R.id.back);
		
		pergs[0]= (RadioGroup) findViewById(R.id.grupoPergunta101);
		pergs[1]= (RadioGroup) findViewById(R.id.grupoPergunta1012);
				
		semFilhos= (RadioButton) findViewById(R.id.pergunta101resposta1);
		ControlaSlides.printKeys();
		resps= ControlaSlides.getRespsPagina(idPag);
		
		if(resps!= null){
			System.out.println("resps naum vazio");
			for(int i=0; i< qtdPerguntas; i++){			
				pergs[i].check(resps[i]);
				
			}
			if(semFilhos.isChecked()){
				/*pergs[1].setEnabled(false);
				qtdPerguntas--;*/
				qtdPerguntas= 1;
			}
		}else{
			resps= new int[qtdPerguntas];
			System.out.println("resps vazio");
		}
		
		for(int i=0; i< qtdPerguntas; i++){			
			pergs[i].check(resps[i]);
			Log.d("respostavinda"+i,""+resps[i]);
		}		
		
		semFilhos.setOnClickListener(new Button.OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				Log.d("CLICK","cliquei");
				//Check
				/*pergs[1].setEnabled(false);
				qtdPerguntas--;*/
				qtdPerguntas= 1;
			}
		});
				
		next.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				next();
			}
		});
				
		back.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				back();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu., menu);
		return true;
	}
	
	float startX = 0, endX= 0;
	public boolean onTouchEvent(MotionEvent event) {
		//dumpEvent(event);
		
		switch (event.getAction() & MotionEvent.ACTION_MASK) {

		case MotionEvent.ACTION_DOWN:
			startX= event.getX();
			//Log.d("MOVE", ">>");
			return true;
		case MotionEvent.ACTION_UP:
			endX= event.getX();
			if(endX>startX){
				/*Log.d("MOVE", "<<"+startX+", "+ endX);
				Intent intent = new Intent(this, MainActivity.class);				
				//**
				startActivity(intent);
				overridePendingTransition(android.R.anim.slide_out_right, android.R.anim.slide_in_left);
				//overridePendingTransition(android.R.anim.
				finish();*/
			}else{
				
			}
			//Log.d("MOVE", ">>");
			return true;	
			
		}
		return false;
	}

	@Override
	public void setqtdPerguntas(int qtd) {
		this.qtdPerguntas= qtd;
	}

	@Override
	public boolean verifyQtdPerguntas() {
		
		for(int i=0; i< qtdPerguntas; i++){
			int resp= pergs[i].getCheckedRadioButtonId();
			Log.d("check "+i,""+resp);
			resps[i]= resp;
			Log.d("saved "+i,""+resp);
			if(resp <=0){
				return false;
			}
		}
		ControlaSlides.addRespsPagina(idPag, resps);
		//ControlaSlides.printKeys();
		return true;
	}

	@Override
	public boolean next(){	
		if(!semFilhos.isChecked()){
			qtdPerguntas= 2;
		}
		if(verifyQtdPerguntas()){			
			Intent intent= new Intent(this, Pag3.class);			
			startActivity(intent);
			finish();					
			return true;
		}else{
			ControlaSlides.NotificaUsuario(this);
			return false;
		}
	}

	@Override
	public boolean back() {
		Intent intent = new Intent(this, Pag2.class);		
		startActivity(intent);		
		finish();
		return true;
	}
}
